package com.practice.search;


public class Trie {
	private Trie[] 	children;
	private char[]	keySet;
	
	public Trie() {
		children = new Trie[26];
	}
	
	public void addWord(String word) {
		char[] c = word.toCharArray();
		Trie parent = this;
		for (int i=0; i<c.length; i++) {
			if (parent.getNode(c[i]) == null) {
				parent.children[c[i]-'a'] = new Trie();
			}
			parent = parent.getNode(c[i]);
		}
	}
	
	public Trie getNode(char c) {
		return children[c-'a'];
	}
	
	public void processKeySet() {
		StringBuilder sb = new StringBuilder();
		for (int i=0; i<26; i++) {
			if (children[i] != null) {
				children[i].processKeySet();
				sb.append((char)('a' + i));
			}
		}
		keySet = sb.toString().toCharArray();
	}
	
	public char[] keySet() {
		return keySet;
	}
}
